// pages/myCoupons/myCoupons.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    choo_modal: "none",
    radioValue: 1,
    checked: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight,
      type: options.type
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    if (that.data.type == 0 || that.data.type==undefined) {
      wx.request({
        url: app.globalData.url + 'getUserNotUsedGiftCard',
        method: 'GET',
        data: {
          userId: wx.getStorageSync("userId"),
        },
        success: function (res) {
          that.setData({
            giftCard: res.data.data.reverse()
          })
        },
      });
    } else {
      wx.request({
        url: app.globalData.url + 'getUserUsedGiftCard',
        method: 'GET',
        data: {
          userId: wx.getStorageSync("userId"),
        },
        success: function (res) {
          that.setData({
            giftCard: res.data.data.reverse()
          })
        },
      });
    }
    
  },
  // 点击tab切换view
  city: function (e) {
    var userid = wx.getStorageSync("userId");
    var that = this;
    if (this.data.type === e.target.dataset.type) {
      return false;
    } else {
      that.setData({
        type: e.target.dataset.type,
      })
    }
    if (e.target.dataset.type==0){
      wx.request({
        url: app.globalData.url + 'getUserNotUsedGiftCard',
        method: 'GET',
        data: {
          userId: wx.getStorageSync("userId"),
        },
        success: function (res) {
          that.setData({
            giftCard: res.data.data
          })
        },
      });
    } else {
      wx.request({
        url: app.globalData.url + 'getUserUsedGiftCard',
        method: 'GET',
        data: {
          userId: wx.getStorageSync("userId"),
        },
        success: function (res) {
          that.setData({
            giftCard: res.data.data
          })
        },
      });
    }
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onReady();
    var that = this;
    const pages = getCurrentPages()
    const currPage = pages[pages.length - 1]
    wx.request({
      url: app.globalData.url + "getUserByWechatUnoinId",
      method: "GET",
      data: {
        wechatUnoinId: wx.getStorageSync("wechatUnoinId"),
        token: that.data.token
      },
      success: function (res) {
        var data = res.data;
        if (data.result == 1) {
          wx.setStorageSync("balance", data.data.balance.toFixed(2));
          var balance = wx.getStorageSync("balance");
          that.setData({
            balance: balance,
          })
        } else {
          wx.reLaunch({
            url: '/pages/zcuslist/index?partnerUserId=' + that.data.partnerUserId,
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  navBack: function () {
    wx.navigateBack({

    })
  },
  mentionCarDetials:function(e){
    var that = this;
    wx.navigateTo({
      url: '/pages/mentionCarDetials/mentionCarDetials?productid=' + e.currentTarget.dataset.productid + '&usergiftcardid=' + e.currentTarget.dataset.usergiftcardid + '&type=' + that.data.type + '&thumbnailimage=' + e.currentTarget.dataset.thumbnailimage ,
    })
  },
  toDetail:function(e){
    var that = this;
    wx.navigateTo({
      url: '/pages/mentionCarPresent/mentionCarPresent'
    })
  },
})